#include "run.h"

void showHelp(){
    drawTextBox(1, 2, 100, "[Q] Quit this application\0");
    drawTextBox(1, 3, 100, "[H] Toggle between help page and browser\0");
    drawTextBox(1, 4, 100, "[R] [T] walk through the list.\0");
    drawBar(7, "Press [H] to leave the help screen", "[H]elp", GREEN, BLUE);
}

void refreshData(item *wCurrent, int index){
    drawTextBox(16, 2 ,100, "\0");
    printItemContent(wCurrent);
    drawTextBox(16, 3 ,100, "\0");
    printItemCount(wCurrent);
    drawTextBox(16, 4 ,100, "\0");
    printItemLen(wCurrent);
    drawTextBox(16, 5 ,100, "\0");
    printf("%d", index);

}

/** @fn     void guiRun()
    @brief  runs the graphical user interface
    @return void
    */
void uiRun(item *wCurrent){
    guiInit();
    int exit = 0;
    char inchar;
    int help = 0;
    int nf = 0;
    int q = 0;
    //print the first record
    item *wTemp = wCurrent;
    refreshData(wTemp, q);

    //loop until exit signal has been given
    while(!exit){

        //get inchar and set to lowercase
        inchar = getch();
        if(inchar > 64 && inchar < 91){inchar = inchar+32;}

        switch (inchar)
        {
        	case 'q': // quit
                exit++;
        		break;
            case 'h': // help
                if(!help){
                    showHelp();
                    help = 1;
                }else{
                    buildUI(1);
                    refreshData(wTemp, q);
                    help = 0;
                }
                break;
            case 'r': // previous
                if(!help){
                    if(!findPrev(wTemp)){
                        nf = 1;
                        printf("%c", 7);
                    }else{
                        nf = 0;
                        wTemp = findPrev(wTemp);
                    }

                    if(nf){
                        //indicate user upon beginning of list
                        drawBar(tDRAWBAR, "Beginning of list", "[H]elp", GREEN, BLUE);
                    }else{
                        //print the contents of item if wTemp is set
                        q--;
                        refreshData(wTemp, q);
                        drawBar(tDRAWBAR, "You can press [Q] anytime to quit!!!", "[H]elp", GREEN, BLUE);
                    }
                }
                break;
            case 't': //next
                if(!help){
                    //
                    if(!findNext(wTemp)){
                        nf = 1;
                        printf("%c", 7);
                    }else{
                        nf = 0;
                        wTemp = findNext(wTemp);
                    }

                    //
                    if(nf){
                        //indicate user upon end of list
                        drawBar(tDRAWBAR, "End of list", "[H]elp", GREEN, BLUE);
                    }else{
                        //print the contents of item if wTemp is set
                        q++;
                        refreshData(wTemp, q);
                        drawBar(tDRAWBAR, "You can press [Q] anytime to quit!!!", "[H]elp", GREEN, BLUE);
                    }
                }
                break;
        	default:
                drawBar(tDRAWBAR, "Unknown command", "[H]elp", GREEN, BLUE);
        		break;
        }

    }
    guiKill();
}
